package com.czk.dao;

import com.czk.pojo.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/7/1 15:57
 */
public interface CustomerDao extends JpaRepository<Customer, Long>, JpaSpecificationExecutor<Customer> {
    /**
     * jpql查询
     * jpql from customer where custName=?
     *
     * @param custName
     * @return
     */
    @Query(value = "from Customer where custName=?")
    Customer findByName(String custName);

    /**
     * 通过用户名和id查询
     *
     * @param custName
     * @param id
     * @return
     */
    @Query(value = "from Customer where custName=? and id=?")
    Customer findCustomerByCustomerNameAndId(String custName, Long id);

    /**
     * 指定参数位置来查询
     *
     * @param id
     * @param custName
     * @return
     */
    @Query(value = "from Customer where custName=?2 and id=?1")
    Customer findCustomerByCustomerNameAndId(Long id, String custName);

    /**
     * 测试更新
     *
     * @param id
     */
    @Query(value = "update Customer set custName=?2 where userId=?1")
    @Modifying
    // 当前方法时更新操作
    void updateCustomerById(Long id, String custName);

    /**
     * 使用sql查询
     *
     * @return
     */
    @Query(nativeQuery = true, value = "select  * from cst_customer")
    List<Object[]> findAllBySql();

    /**
     * sql模糊查询
     *
     * @param name
     * @return
     */
    @Query(nativeQuery = true, value = "select * from cst_customer where cust_name like ?")
    List<Object[]> likeFindBySql(String name);

    /**
     * 方法名的约定查询
     * findBy查询
     * 对象中的属性名（首字母大写）：查询条件
     * findByCustName  -根据客户名称查询
     * 再springDataJpa的运行阶段
     * 会根据方法名称进行解析 findBy from xxx实体类 where custName=?
     */
    Customer findByCustName(String custName);

    /**
     * 模糊匹配
     *
     * @param custName
     * @return
     */
    Customer findByCustNameLike(String custName);

    /**
     * 多条件组合查询
     *
     * @param custName
     * @param custLevel
     * @return
     */
    Customer findByCustNameLikeAndCustLevel(String custName, String custLevel);
}
